package com.woniuxy.activiti.service;

import com.woniuxy.activiti.mapper.FlowMapper;
import com.woniuxy.entity.FlowInfo;
import com.woniuxy.entity.User;
import com.woniuxy.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.delegate.DelegateTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 工作流
 * @author xilei
 * @create 2021-11-12 16:52
 */
@Slf4j
@Service
public class FlowService {

    @Autowired
    private ActFlowCommService actFlowCommService;
    @Resource
    private FlowMapper flowMapper;

    public List<Map<String,Object>> findUserTask(Integer userId){
        List<Map<String, Object>> list = actFlowCommService.myTaskList(userId.toString());
        return list;
    }

    /**
     * 查询所有流程
     * @return
     */
    public List<FlowInfo> findAllFlow() {
        return flowMapper.selectFlowList();
    }

    /**
     * 查询单个流程
     * @param id
     * @return
     */
    public FlowInfo findOneFlow(String id){
        return flowMapper.selectOneFlow(id);
    }

    /**
     * 更新部署状态
     * @param id
     * @return
     */
    public int updateDeployState(String id){
        return flowMapper.updateFlowDeployState(id);
    }

    /**
     * 查询任务详细信息
     * @param userId
     * @return
     */
    public List<Map<String,Object>> findTaskInfo(int userId){
        List<Map<String, Object>> list = actFlowCommService.myTaskInfoList(String.valueOf(userId));
        return list;
    }

    /**
     * 完成任务
     * @param taskId
     * @param userId
     */
    public void completeTask(String taskId,int userId){
        actFlowCommService.completeProcess("同意", taskId, String.valueOf(userId));
    }

    @Autowired
    private UserService userService;
    @Autowired
    private SiteMessageService siteMessageService;

    /**
     * 任务创建事件
     * @param delegateTask
     */
    public void createTaskEvent(DelegateTask delegateTask){
        log.info("委托任务--delegateTask=={}",delegateTask);
//        负责人
        String assignee = delegateTask.getAssignee();
//        获取当前登录用户
        //String username = SecurityContextHolder.getContext().getAuthentication().getName();
        String username = "张三";
        User user = userService.getUser(username);
        String userId = String.valueOf(user.getId());
//        任务id
        String taskId = delegateTask.getId();
        System.out.println("userId:"+userId);
        System.out.println("taskId:"+taskId);
        if(!assignee.equals(userId)){
            int type =1;
            int i = siteMessageService.sendMsg(Integer.valueOf(assignee), taskId, type, 1);
            System.out.println("i:"+i);
        }
    }
}
